package com.ssm.studen.controller;

import com.ssm.studen.model.Personinfo;
import com.ssm.studen.service.PersoninfoService;
import com.ssm.studen.tool.ResponseMessage;
import com.ssm.studen.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 人员信息表：
 *          对外提供数据服务接口
 * */
@CrossOrigin
@Api(tags = "Personinfo")
@RestController
public class PersoninfoController {

    @Autowired
    private PersoninfoService personinfoService;


    /*****************人员信息添加方法*******************/
    @ApiOperation("人员信息添加方法")
    @RequestMapping(value = "/addPersoninfo",method = RequestMethod.GET)
    @ResponseBody
    public int addPersoninfo(Personinfo personinfo){
        int IsAdd = 0;
        //传入数据判断
        //1.实体对象不能为空
        if(personinfo != null){
            //2.人员信息表重要信息不能为空
            if(personinfo.getPlogin()!=null&&
                    personinfo.getPname()!=null&&
                    personinfo.getPid()!=null&&
                    personinfo.getPpwd()!=null&&
                    personinfo.getPstart()!=null&&
                    personinfo.getPupdate()!=null&&
                    personinfo.getPenable()!=null&&
                    personinfo.getPapproval()!=null){
                //3.添加人员信息数据
                IsAdd = personinfoService.addPersoninfo(personinfo);
            }
        }
        return IsAdd;
    }


    /*****************人员信息删除方法*******************/
    @ApiOperation("人员信息删除方法")
    @RequestMapping(value = "/delectPersoninfo",method = RequestMethod.GET)
    public ResponseMessage delectPersoninfo(int pnum){
        if(pnum != 0){
            int delInt = 0;
            delInt = personinfoService.delPersoninfo(pnum);
            if(delInt>0){
                return Result.success("删除成功");
            }else{
                return Result.error("删除失败");
            }
        }else{
            return Result.error("删除的主键不能为“0”，请重新输入！");
        }
    }


    /*****************人员信息更新方法*******************/
    @ApiOperation("人员信息更新方法")
    @RequestMapping(value = "/updatePersoninfo",method = RequestMethod.GET)
    public ResponseMessage updatePersoninfo(Personinfo personinfo){
        //传入的参数不能为空
        if(personinfo != null){
            //参数当中主键不能为0
            if(personinfo.getPnum() != 0){
                if(personinfoService.updatePersoninfo(personinfo) == 1){
                    if(personinfo.getPlogin()!=null&&
                            personinfo.getPname()!=null&&
                            personinfo.getPid()!=null&&
                            personinfo.getPpwd()!=null&&
                            personinfo.getPstart()!=null&&
                            personinfo.getPupdate()!=null&&
                            personinfo.getPenable()!=null&&
                            personinfo.getPapproval()!=null){
                        personinfoService.updatePersoninfo(personinfo);
                    }
                    return Result.success("更新成功");
                }else{
                    return Result.error("更新失败");
                }
            }
        }
        return Result.error("传入参数不正确");
    }


    /**********************人员信息全部查询***********************/
    @ApiOperation("人员信息全部查询方法")
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public String listUser(Model model) {
        List<Personinfo> personinfos = personinfoService.findAll();
        model.addAttribute("personinfos", personinfos);
        return "listPersoninfo";
    }



    /*****************人员信息查询方法*******************/
    @ApiOperation("人员信息查询方法")
    @RequestMapping(value = "/selectPersoninfo",method = RequestMethod.GET)
    public ResponseMessage selectPersoninfo(@RequestParam int pnum){
        Map<String,List> map = new HashMap<>();
        List<Personinfo> listPersoninfo = personinfoService.selectPersoninfo(pnum);
        map.put("list",listPersoninfo);
        return Result.success("200",map);
    }
}










